package com.waterfallscroll.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;


import com.waterfallscroll.view.adapter.WaterfallFlowAdapter;
import com.waterfallscroll.view.bean.ImageInfoBean;
import com.waterfallscroll.view.utils.ImageManageUtil;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.waterfallscroll.view.CustomWaterfallScrollView.ColumnNum.*;

/**
 * 自定义的ScrollView，在其中动态地对图片进行添加。
 * 
 * @author guolin
 *
 */
public class CustomWaterfallScrollView extends ScrollView implements OnTouchListener {
	/** 每一列的宽度 */
	private int perColumnWidth;
	/** 当前第一列的高度 */
	private int firstColumnHeight;
	/** 当前第二列的高度 */
	private int secondColumnHeight;
	/** 当前第三列的高度 */
	private int thirdColumnHeight;
	/** 是否已加载过一次layout，这里onLayout中的初始化只需加载一次 */
	private boolean loadOnce = false;
	/** 对图片进行管理的工具类 */
	private ImageManageUtil imageLoader;
	/** 第一列的布局 */
	private LinearLayout lLayout_firstColumn;
	/** 第二列的布局 */
	private LinearLayout lLayout_secondColumn;
	/** 第三列的布局 */
	private LinearLayout lLayout_thirdColumn;
	/** 记录所有正在下载或等待下载的任务。 */
	public static Set<LoadImageTask> taskCollection;
	/** ScrollView下的直接子布局。 */
	private static View childLayout;
	/** ScrollView布局的高度。 */
	private static int scrollViewHeight;
	/** 记录上垂直方向的滚动距离。 */
	private static int lastScrollY = -1;
	/** 记录所有界面上的图片，用以可以随时控制对图片的释放。 */
	private List<View> imageViewList = new ArrayList<View>();
	/** 在Handler中进行图片可见性检查的判断，以及加载更多图片的操作。 */
	private WaterfallFlowAdapter waterfallBaseAdapter;
	/** */
	private static OnWaterfallFlowScrollingListener flowScrollingListener;
	/** */
	private static Context context;
	/** 列数 默认为两列*/
	private ColumnNum columnNum = TWO;
	/** 每个item宽度 */
	private int itemViewWidth;
	/** 每个item高度*/
	private int itemViewHeight;
	/** 处理滚动状态*/
	private static Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			CustomWaterfallScrollView myScrollView = (CustomWaterfallScrollView) msg.obj;
			int scrollY = myScrollView.getScrollY();
			// 如果当前的滚动位置和上次相同，表示已停止滚动
			if (scrollY == lastScrollY) {
				// 当滚动的最底部，并且当前没有正在下载的任务时，开始加载下一页的图片
				boolean isToBottom = ((scrollViewHeight + scrollY >= childLayout.getHeight()) && (taskCollection.isEmpty())) == true ? true : false;
				if (flowScrollingListener != null && isToBottom){
					if (flowScrollingListener.waterfallFlowScrollingListener(isToBottom)){
						myScrollView.loadMoreImages();
					}else {
						Toast.makeText(context, "已没有更多图片", Toast.LENGTH_SHORT).show();
					}
				}
				myScrollView.checkVisibility();
			} else {
				lastScrollY = scrollY;
				Message message = new Message();
				message.obj = myScrollView;
				// 5毫秒后再次对滚动位置进行判断
				handler.sendMessageDelayed(message, 5);
			}
		};

	};



	public CustomWaterfallScrollView(Context context) {
		this(context, null);
	}

	/**
	 * 有参构造方法
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,10:47
	 * <h3>UpdateTime</h3> 2016/1/14,10:47
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @param context
	 * @param attrs
	 */
	public CustomWaterfallScrollView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);

	}

	public CustomWaterfallScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.waterfallColumnNum);
		int columnNum = typedArray.getInteger(R.styleable.waterfallColumnNum_columnNum,2);
		switch (columnNum){
			case 1:
				setColumnNum(ONE);
				break;
			case 2:
				setColumnNum(TWO);
				break;
			case 3:
				setColumnNum(THREE);
				break;
		}
		this.context = context;
		imageLoader = ImageManageUtil.getInstance();
		taskCollection = new HashSet<LoadImageTask>();
		setOnTouchListener(this);
		if (!loadOnce){
			addLayout(context);
		}
	}

	/**
	 * 枚举类 列数有三种模式：一列瀑布流  二列瀑布  三瀑布流 默认为二列
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/15,13:50
	 * <h3>UpdateTime</h3> 2016/1/15,13:50
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 */
	public enum ColumnNum{
		ONE,TWO,THREE
	}
	/**
	 * 添加布局
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,16:54
	 * <h3>UpdateTime</h3> 2016/1/14,16:54
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @param context
	 */
	private void addLayout(Context context) {
		LinearLayout.LayoutParams parentParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		LinearLayout parentLayout = new LinearLayout(context);
		parentLayout.setLayoutParams(parentParams);
		parentLayout.setOrientation(LinearLayout.HORIZONTAL);
		addView(parentLayout);

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		layoutParams.weight = 1;

		switch (columnNum){
			case ONE:
				lLayout_firstColumn = new LinearLayout(context);
				lLayout_firstColumn.setLayoutParams(layoutParams);
				lLayout_firstColumn.setOrientation(LinearLayout.VERTICAL);
				parentLayout.addView(lLayout_firstColumn);
				break;
			case TWO:
				lLayout_firstColumn = new LinearLayout(context);
				lLayout_firstColumn.setLayoutParams(layoutParams);
				lLayout_firstColumn.setOrientation(LinearLayout.VERTICAL);
				parentLayout.addView(lLayout_firstColumn);
				lLayout_secondColumn = new LinearLayout(context);
				lLayout_secondColumn.setLayoutParams(layoutParams);
				lLayout_secondColumn.setOrientation(LinearLayout.VERTICAL);
				parentLayout.addView(lLayout_secondColumn);
				break;
			case THREE:
				lLayout_firstColumn = new LinearLayout(context);
				lLayout_firstColumn.setLayoutParams(layoutParams);
				lLayout_firstColumn.setOrientation(LinearLayout.VERTICAL);
				parentLayout.addView(lLayout_firstColumn);
				lLayout_secondColumn = new LinearLayout(context);
				lLayout_secondColumn.setLayoutParams(layoutParams);
				lLayout_secondColumn.setOrientation(LinearLayout.VERTICAL);
				parentLayout.addView(lLayout_secondColumn);
				lLayout_thirdColumn = new LinearLayout(context);
				lLayout_thirdColumn.setLayoutParams(layoutParams);
				lLayout_thirdColumn.setOrientation(LinearLayout.VERTICAL);
				parentLayout.addView(lLayout_thirdColumn);
				break;
		}
	}

	/**
	 * 设置适配器
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,12:37
	 * <h3>UpdateTime</h3> 2016/1/14,12:37
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @param adapter
	 */
	public void setAdapter(WaterfallFlowAdapter adapter){
		if (adapter != null){
			this.waterfallBaseAdapter = adapter;
		}
	}


	/**
	 * 设置列数目前只支持到三列
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/15,14:04
	 * <h3>UpdateTime</h3> 2016/1/15,14:04
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 * @param columnNum 列数 值：ONE，TWO,THREE 一列 ， 两列 ，三列
	 */
	private void setColumnNum(ColumnNum columnNum){
		this.columnNum = columnNum;
	}

	/**
	 * 进行一些关键性的初始化操作，获取ScrollView的高度，以及得到第一列的宽度值。并在这里开始加载第一页的图片。
	 */
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		if (changed && !loadOnce) {
				scrollViewHeight = getHeight();
				childLayout = getChildAt(0);
				perColumnWidth = lLayout_firstColumn.getWidth();
				loadOnce = true;
				loadMoreImages();
		}
	}
	/**
	 * 监听用户的触屏事件，如果用户手指离开屏幕则开始进行滚动检测。
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			Message message = new Message();
			message.obj = this;
			handler.sendMessageDelayed(message, 5);
		}
		return false;
	}

	/**
	 * 开始加载下一页的图片，每张图片都会开启一个异步线程去下载。
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,10:56
	 * <h3>UpdateTime</h3> 2016/1/14,10:56
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 */
	public void loadMoreImages() {
		if (hasSDCard()) {
			if (waterfallBaseAdapter != null) {
				Toast.makeText(getContext(), "正在加载...", Toast.LENGTH_SHORT).show();
				for (int i = 0; i < waterfallBaseAdapter.getItemsCount(); i++) {
					LoadImageTask task = new LoadImageTask(waterfallBaseAdapter.getItem(i));
					taskCollection.add(task);
					task.execute(waterfallBaseAdapter.getItem(i).getImageUrl());
				}
			} else {
				Toast.makeText(getContext(), "已没有更多图片", Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(getContext(), "未发现SD卡", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 遍历imageViewList中的每张图片，对图片的可见性进行检查，如果图片已经离开屏幕可见范围，则将图片替换成一张空图。
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,10:58
	 * <h3>UpdateTime</h3> 2016/1/14,10:58
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 */
	public void checkVisibility() {
		for (int i = 0; i < imageViewList.size(); i++) {
			View itemView = imageViewList.get(i);
			ImageView imageView;
			if (itemView != null){
				imageView = waterfallBaseAdapter.getImageViewOfItemView2(itemView);
			}else {
				imageView = (ImageView) imageViewList.get(i);
			}
			int borderTop = (Integer) imageView.getTag(R.string.border_top);
			int borderBottom = (Integer) imageView.getTag(R.string.border_bottom);
			if (borderBottom > getScrollY() && borderTop < getScrollY() + scrollViewHeight) {
				String imageUrl = (String) imageView.getTag(R.string.image_url);
				Bitmap bitmap = imageLoader.getBitmapFromMemoryCache(imageUrl);
				if (bitmap != null) {
					imageView.setImageBitmap(bitmap);
				} else {
					LoadImageTask task = new LoadImageTask(imageView);
					task.execute(imageUrl);
				}
			} else {
				imageView.setImageResource(R.drawable.empty_photo);
			}
		}
	}

	/**
	 * 判断手机是否有SD卡。
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,10:59
	 * <h3>UpdateTime</h3> 2016/1/14,10:59
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @return 有SD卡返回true，没有返回false。
	 */
	private boolean hasSDCard() {
		return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
	}

	/**
	 * 异步下载图片的任务。
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,10:59
	 * <h3>UpdateTime</h3> 2016/1/14,10:59
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @author guolin
	 */
	class LoadImageTask extends AsyncTask<String, Void, Bitmap> {
		/** 图片的URL地址 */
		private String mImageUrl;
		/** 可重复使用的ImageView */
		private ImageView mImageView;

		private ImageInfoBean imageInfoBean;
		/**
		 * 无参的构造方法
		 * <h3>Version</h3> 1.0
		 * <h3>CreateTime</h3> 2016/1/14,11:00
		 * <h3>UpdateTime</h3> 2016/1/14,11:00
		 * <h3>CreateAuthor</h3> luzhenbang
		 * <h3>UpdateAuthor</h3>
		 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
		 *
		 */
		public LoadImageTask(ImageInfoBean imageInfoBean) {
			this.imageInfoBean = imageInfoBean;
		}

		/**
		 *
		 * 有参的构造方法
		 * <h3>Version</h3> 1.0
		 * <h3>CreateTime</h3> 2016/1/14,11:00
		 * <h3>UpdateTime</h3> 2016/1/14,11:00
		 * <h3>CreateAuthor</h3> luzhenbang
		 * <h3>UpdateAuthor</h3>
		 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
		 *
		 * @param imageView 可重复使用的ImageView
		 */
		public LoadImageTask(ImageView imageView) {
			this.mImageView = imageView;
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			mImageUrl = params[0];
			Bitmap imageBitmap = imageLoader.getBitmapFromMemoryCache(mImageUrl);
			if (imageBitmap == null) {
				imageBitmap = loadImage(mImageUrl);
			}
			return imageBitmap;
		}

		@Override
		protected void onPostExecute(Bitmap bitmap) {
			if (bitmap != null) {
				double ratio = bitmap.getWidth() / (perColumnWidth * 1.0);
				int scaledHeight = (int) (bitmap.getHeight() / ratio);
				addImage(bitmap, perColumnWidth, scaledHeight,mImageView,mImageUrl ,imageInfoBean);
			}
			taskCollection.remove(this);
		}

		/**
		 * 根据传入的URL，对图片进行加载。如果这张图片已经存在于SD卡中，则直接从SD卡里读取，否则就从网络上下载。
		 *
		 * <h3>Version</h3> 1.0
		 * <h3>CreateTime</h3> 2016/1/14,11:10
		 * <h3>UpdateTime</h3> 2016/1/14,11:10
		 * <h3>CreateAuthor</h3> luzhenbang
		 * <h3>UpdateAuthor</h3>
		 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
		 *
		 * @param imageUrl
		 *            图片的URL地址
		 * @return 加载到内存的图片。
		 */
		private Bitmap loadImage(String imageUrl) {
			File imageFile = new File(getImagePath(imageUrl));
			if (!imageFile.exists()) {
				downloadImage(imageUrl);
			}
			if (imageUrl != null) {
				Bitmap bitmap = ImageManageUtil.decodeSampledBitmapFromResource(imageFile.getPath(), perColumnWidth);
				if (bitmap != null) {
					imageLoader.addBitmapToMemoryCache(imageUrl, bitmap);
					return bitmap;
				}
			}
			return null;
		}
		/**
		 * 将图片下载到SD卡缓存起来。
		 *
		 * <h3>Version</h3> 1.0
		 * <h3>CreateTime</h3> 2016/1/14,11:13
		 * <h3>UpdateTime</h3> 2016/1/14,11:13
		 * <h3>CreateAuthor</h3> luzhenbang
		 * <h3>UpdateAuthor</h3>
		 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
		 *
		 * @param imageUrl
		 *            图片的URL地址。
		 */
		private void downloadImage(String imageUrl) {
			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			} else {
			}
			HttpURLConnection con = null;
			FileOutputStream fos = null;
			BufferedOutputStream bos = null;
			BufferedInputStream bis = null;
			File imageFile = null;
			try {
				URL url = new URL(imageUrl);
				con = (HttpURLConnection) url.openConnection();
				con.setConnectTimeout(5 * 1000);
				con.setReadTimeout(15 * 1000);
				con.setDoInput(true);
				con.setDoOutput(true);
				bis = new BufferedInputStream(con.getInputStream());
				imageFile = new File(getImagePath(imageUrl));
				fos = new FileOutputStream(imageFile);
				bos = new BufferedOutputStream(fos);
				byte[] b = new byte[1024];
				int length;
				while ((length = bis.read(b)) != -1) {
					bos.write(b, 0, length);
					bos.flush();
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (bis != null) {
						bis.close();
					}
					if (bos != null) {
						bos.close();
					}
					if (con != null) {
						con.disconnect();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (imageFile != null) {
				Bitmap bitmap = ImageManageUtil.decodeSampledBitmapFromResource(imageFile.getPath(), perColumnWidth);
				if (bitmap != null) {
					imageLoader.addBitmapToMemoryCache(imageUrl, bitmap);
				}
			}
		}

		/**
		 * 获取图片的本地存储路径。
		 *
		 * <h3>Version</h3> 1.0
		 * <h3>CreateTime</h3> 2016/1/14,11:09
		 * <h3>UpdateTime</h3> 2016/1/14,11:09
		 * <h3>CreateAuthor</h3> luzhenbang
		 * <h3>UpdateAuthor</h3>
		 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
		 *
		 * @param imageUrl
		 *            图片的URL地址。
		 * @return 图片的本地存储路径。
		 */
		private String getImagePath(String imageUrl) {
			int lastSlashIndex = imageUrl.lastIndexOf("/");
			String imageName = imageUrl.substring(lastSlashIndex + 1);
			String imageDir = Environment.getExternalStorageDirectory().getPath() + "/PhotoWallFalls/";
			File file = new File(imageDir);
			if (!file.exists()) {
				file.mkdirs();
			}
			String imagePath = imageDir + imageName;
			return imagePath;
		}
	}



	/**
	 * 向ImageView中添加一张图片
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,11:10
	 * <h3>UpdateTime</h3> 2016/1/14,11:10
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *  @param bitmap
	 *            待添加的图片
	 * @param imageWidth
	 *            图片的宽度
	 * @param imageHeight
	 * @param mImageView
	 */
	private void addImage(Bitmap bitmap, final int imageWidth, final int imageHeight, ImageView mImageView,String mImageUrl ,ImageInfoBean imageInfoBean) {
		itemViewWidth = imageWidth;
		itemViewHeight = imageHeight;

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(imageWidth, imageHeight);
		if (mImageView != null) {
			mImageView.setImageBitmap(bitmap);
		} else {
			final View view = waterfallBaseAdapter.getItemView();
			if (view != null){
				waterfallBaseAdapter.initChildrenViewOfItemView(view,imageInfoBean);
				view.setTag(R.string.view_data,imageInfoBean);
				view.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (onWaterfallFlowItemClickListener != null) {
							onWaterfallFlowItemClickListener.WaterfallFlowItemClickListener((ImageInfoBean) view.getTag(R.string.view_data));
						}
						if (imageWidth < v.getWidth()){
                            itemViewWidth = v.getWidth();
						}
						if (imageHeight < v.getHeight()){
							itemViewHeight = v.getHeight();
						}
					}
				});
				imageViewList.add(view);
			}

			ImageView imageView = waterfallBaseAdapter.getImageViewOfItemView2(view);
			if (imageView != null){
				imageView.setLayoutParams(params);
				imageView.setImageBitmap(bitmap);
				imageView.setScaleType(ScaleType.FIT_XY);
				imageView.setPadding(5, 5, 5, 5);
				imageView.setTag(R.string.image_url, mImageUrl);
				if (view == null){
					imageViewList.add(imageView);
					switch (columnNum){
						case ONE:
                            findOneColumnToAdd(imageView,itemViewHeight).addView(imageView);
							break;
						case TWO:
							findTwoColumnToAdd(imageView,itemViewHeight).addView(imageView);
							break;
						case THREE:
							findThreeColumnToAdd(imageView, itemViewHeight).addView(imageView);
							break;
					}
				} else {

					switch (columnNum){
						case ONE:
							findOneColumnToAdd(imageView,itemViewHeight).addView(view);
							break;
						case TWO:
							findTwoColumnToAdd(imageView,itemViewHeight).addView(view);
							break;
						case THREE:
							findThreeColumnToAdd(imageView, itemViewHeight).addView(view);
							break;
					}

				}

			}

		}
	}

	/**
	 * 找到此时应该添加图片的一列。原则就是对三列的高度进行判断，当前高度最小的一列就是应该添加的一列。
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,11:13
	 * <h3>UpdateTime</h3> 2016/1/14,11:13
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @param imageView
	 * @param imageHeight
	 * @return 应该添加图片的一列
	 */
	private LinearLayout findThreeColumnToAdd(ImageView imageView, int imageHeight) {
		if (firstColumnHeight <= secondColumnHeight) {//第一列的高度小于第二列的高度
			if (firstColumnHeight <= thirdColumnHeight) {//第一列的高度小于第三列的高度
				imageView.setTag(R.string.border_top, firstColumnHeight);
				firstColumnHeight += imageHeight;//第一列的高度加图片的高度
				imageView.setTag(R.string.border_bottom, firstColumnHeight);
				return lLayout_firstColumn;
			}
			////第一列的高度大于第三列的高度
			imageView.setTag(R.string.border_top, thirdColumnHeight);
			thirdColumnHeight += imageHeight;
			imageView.setTag(R.string.border_bottom, thirdColumnHeight);
			return lLayout_thirdColumn;
		} else {//第一列的高度大于第二列的高度
			if (secondColumnHeight <= thirdColumnHeight) {//第二列的高度小于第三列的高度
				imageView.setTag(R.string.border_top, secondColumnHeight);
				secondColumnHeight += imageHeight;
				imageView.setTag(R.string.border_bottom, secondColumnHeight);
				return lLayout_secondColumn;
			}
			//第一列的高度大于第三列的高度
			imageView.setTag(R.string.border_top, thirdColumnHeight);
			thirdColumnHeight += imageHeight;
			imageView.setTag(R.string.border_bottom, thirdColumnHeight);
			return lLayout_thirdColumn;
		}
	}




	/**
	 * 找到此时应该添加图片的一列。原则就是对两列的高度进行判断，当前高度最小的一列就是应该添加的一列。
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,11:13
	 * <h3>UpdateTime</h3> 2016/1/14,11:13
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @param imageView
	 * @param imageHeight
	 * @return 应该添加图片的一列
	 */
	private LinearLayout findTwoColumnToAdd(ImageView imageView, int imageHeight) {
		if (firstColumnHeight <= secondColumnHeight) {//第一列的高度小于第二列的高度
			imageView.setTag(R.string.border_top, firstColumnHeight);
			firstColumnHeight += imageHeight;
			imageView.setTag(R.string.border_bottom, firstColumnHeight);
			return lLayout_firstColumn;
		} else {//第一列的高度大于第二列的高度
			imageView.setTag(R.string.border_top, secondColumnHeight);
			secondColumnHeight += imageHeight;
			imageView.setTag(R.string.border_bottom, secondColumnHeight);
			return lLayout_secondColumn;
		}
	}


	/**
	 * 找到此时应该添加图片的一列。原则就是对两列的高度进行判断，当前高度最小的一列就是应该添加的一列。
	 *
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,11:13
	 * <h3>UpdateTime</h3> 2016/1/14,11:13
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @param imageView
	 * @param imageHeight
	 * @return 应该添加图片的一列
	 */
	private LinearLayout findOneColumnToAdd(ImageView imageView, int imageHeight) {
		imageView.setTag(R.string.border_top, firstColumnHeight);
		firstColumnHeight += imageHeight;
		imageView.setTag(R.string.border_bottom, firstColumnHeight);
		return lLayout_firstColumn;
	}


	/**
	 * 通过手机屏幕的大小，设置图片的大小。
	 * @param imageViewShower 要被设置图片大小的控件。
	 */
	private void setImageSize(ImageView imageViewShower) {
        /*获取手机屏幕分辨率信息的类*/
		DisplayMetrics displayMetrics = new DisplayMetrics();
		((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        /*手机屏幕的宽度*/
		int widthScreen = displayMetrics.widthPixels;
        /*手机屏幕的高度*/
		int heightScreen = displayMetrics.heightPixels;
        /*获取图片的参数*/
		ViewGroup.LayoutParams layoutParamsImView = imageViewShower.getLayoutParams();
       /*设置图片的宽度*/
		layoutParamsImView.width = (int)(widthScreen * 0.8f);
       /*设置图片的高度*/
		layoutParamsImView.height = (int)(heightScreen * 0.8f) ;
		imageViewShower.setLayoutParams(layoutParamsImView);
	}
	/**
	 *
	 * 滑动监听
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,15:49
	 * <h3>UpdateTime</h3> 2016/1/14,15:49
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 */

	public interface OnWaterfallFlowScrollingListener{
		/**
		 * 对滑动状态进行监听
		 * <h3>Version</h3> 1.0
		 * <h3>CreateTime</h3> 2016/1/14,15:55
		 * <h3>UpdateTime</h3> 2016/1/14,15:55
		 * <h3>CreateAuthor</h3> luzhenbang
		 * <h3>UpdateAuthor</h3>
		 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
		 * @param isToBottom 是否滑动到底部
		 * @return true 还有数据加载 false 没有数据加载了
		 */
		public boolean waterfallFlowScrollingListener(boolean isToBottom);
	}


	/**
	 * 设置监听
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,15:55
	 * <h3>UpdateTime</h3> 2016/1/14,15:55
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 * @param waterfallFlowScrollingListener
	 */
	public void setOnWaterfallFlowScrollingListener(OnWaterfallFlowScrollingListener waterfallFlowScrollingListener){
		if (waterfallFlowScrollingListener != null){
			this.flowScrollingListener = waterfallFlowScrollingListener;
		}
	}

	/**
	 *
	 * 自定义接口对item进行点击监听
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,15:49
	 * <h3>UpdateTime</h3> 2016/1/14,15:49
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 *
	 */
	public interface OnWaterfallFlowItemClickListener{
		public void WaterfallFlowItemClickListener(ImageInfoBean imageInfoBean);
	}

	private OnWaterfallFlowItemClickListener onWaterfallFlowItemClickListener;

	/**
	 * 设置item监听
	 * <h3>Version</h3> 1.0
	 * <h3>CreateTime</h3> 2016/1/14,16:31
	 * <h3>UpdateTime</h3> 2016/1/14,16:31
	 * <h3>CreateAuthor</h3> luzhenbang
	 * <h3>UpdateAuthor</h3>
	 * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
	 * @param onWaterfallFlowItemClickListener
	 */
	public void setOnWaterfallFlowItemClickListener(OnWaterfallFlowItemClickListener onWaterfallFlowItemClickListener){
		this.onWaterfallFlowItemClickListener = onWaterfallFlowItemClickListener;
	}

}